﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBM_Commander.Common.Encoding;

namespace PETSCII__ASCII_Converter
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnSelectSourceFile_Click(object sender, EventArgs e)
        {
            dlgOpen.DefaultExt = ".txt";
            dlgOpen.ShowDialog(this);
            txtSource.Text = dlgOpen.FileName;
        }

        private void btnSelectTargetFile_Click(object sender, EventArgs e)
        {
            dlgSave.DefaultExt = ".txt";
            dlgSave.ShowDialog(this);
            txtTargetFile.Text = dlgSave.FileName;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if(ddlTarget.SelectedIndex > -1)
            {
                if (File.Exists(txtSource.Text))
                {
                    var file = File.Open(txtSource.Text, FileMode.Open);
                    int length = Convert.ToInt32(file.Length);
                    file.Close();
                    string targetString = null;
                    byte[] sourceBytes = new byte[length];
                    File.OpenRead(txtSource.Text).Read(sourceBytes, 0, length);
                    if (ddlTarget.SelectedIndex == 0)
                    {
                        targetString = ASCIIEncoding.ASCII.GetString(
                            Petscii.Convert(new Petscii(), new ASCIIEncoding(), sourceBytes));

                        rtxtOutput.Text = targetString;
                        var ascii = new ASCIIEncoding();
                        var outFile = File.OpenWrite(txtTargetFile.Text);
                        outFile.Write(
                            ascii.GetBytes(targetString), 0, ascii.GetByteCount(targetString));
                        outFile.Close();
                    }
                    else
                    {
                        targetString =
                            ASCIIEncoding.ASCII.GetString(Petscii.Convert(new ASCIIEncoding(), new Petscii(),
                                                                          sourceBytes));

                        rtxtOutput.Text = targetString;
                        var petscii = new Petscii();
                        var outFile = File.OpenWrite(txtTargetFile.Text);
                        outFile.Write(
                            petscii.GetBytes(targetString), 0, petscii.GetByteCount(targetString));
                        outFile.Close();
                    }
                }
                else
                {
                    MessageBox.Show(this,
                                    "Source file does not exist.",
                                    "Conversion Error",
                                    MessageBoxButtons.OK);
                }
            }
            else
            {
                MessageBox.Show(this,
                                "Must select a target format.",
                                "Conversion Error",
                                MessageBoxButtons.OK);
            }
        }
    }
}
